const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  TheClass,
} = require('../db/model');

class TheClassService extends Service {
  async getAllTheClass() {
    const res = await TheClass.findAll().catch(err => {
      console.log(`TheClass 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`TheClass 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getTheClass(theClass) {
    if (!theClass?.tcId) {
      console.log(`TheClass 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${theClass}`, theClass);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    const res = await TheClass.findOne({
      where: {
        tcId: theClass?.tcId || -1
      },
    }).catch(err => {
      console.log(`TheClass 表查找失败: ${err}\n参数: ${theClass}`, theClass);
    });
    if (res === undefined) {
      console.log(`TheClass 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${theClass}`, theClass);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`TheClass 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${theClass}`, theClass);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendTheClass(theClass) {
    let res = await this.getTheClass(theClass);
    // 会返回 'PARAMETERS_MISSING' 'DATABASE_ERROR' 'PRIMARY_KEY_NOT_EXIST' theClass
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`TheClass 表新增失败: ${res}\n参数: ${theClass}`, theClass);
      return res;
    }
    res = await TheClass.create(theClass).catch(err => {
      console.log(`TheClass 表新增失败: ${err}\n参数: ${theClass}`, theClass);
    });
    if (res === undefined) {
      console.log(`TheClass 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${theClass}`, theClass);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateTheClass(theClass) {
    let res = await this.getTheClass(theClass);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`TheClass 表更新失败: ${res}\n参数: ${theClass}`, theClass);
      return res;
    }
    res = await TheClass.update(theClass, {
      where: {
        tcId: theClass?.tcId || -1
      },
    }).catch(err => {
      console.log(`TheClass 表更新失败: ${err}\n参数: ${theClass}`, theClass);
    });
    if (res === undefined) {
      console.log(`TheClass 表更新失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${theClass}`, theClass);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyTheClass(theClass) {
    let res = await this.getTheClass(theClass);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`TheClass 表删除失败: ${res}\n参数: ${theClass}`, theClass);
      return res;
    }
    res = await TheClass.destroy({
      where: {
        tcId: theClass?.tcId || -1
      },
    }).catch(err => {
      console.log(`TheClass 表删除失败: ${err}\n参数: ${theClass}`, theClass);
    });
    if (res === undefined) {
      console.log(`TheClass 表删除失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${theClass}`, theClass);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = TheClassService;
